package com.future.mayi.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.ToastUtil;
import com.future.mayi.R;
import com.future.mayi.activity.GoodsActivity;
import com.future.mayi.model.GoodsModel;
import com.future.mayi.model.SpecListModel;
import com.future.mayi.model.interfaces.GoodsSelectChangeListener;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.PriceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsSkuView extends LinearLayout implements GoodsSelectChangeListener {
    @BindView(R.id.rl_main_view)
    View rlMainView;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_remaining)
    TextView tvRemaining;
    @BindView(R.id.gsv)
    GoodsSelectView gsv;
    @BindView(R.id.scroll_view)
    ScrollView scrollView;
    @BindView(R.id.tv_sub)
    TextView tvSub;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.iv_close)
    ImageView ivClose;

    private Context mContext;

    public GoodsSkuView(Context context) {
        this(context, null);
    }

    public GoodsSkuView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsSkuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        dismiss();
        mContext = context;
        View.inflate(mContext, R.layout.dialog_goods_select, this);
        ButterKnife.bind(this);
        tvNum.setCursorVisible(false);
        tvNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!TextUtils.isEmpty(tvNum.getText()))
                    tvNum.setSelection(tvNum.getText().toString().length());
            }
        });
        gsv.setListener(this);
        setData(cacheGoodsModel, addCart, rlMainView);
    }


    @OnClick({R.id.tv_sub, R.id.tv_add, R.id.tv_enter, R.id.iv_close})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_sub: {
                if (TextUtils.isEmpty(tvNum.getText())) {
                    tvNum.setText("1");
                    return;
                }
                int num = Integer.valueOf(tvNum.getText().toString()) - 1;
                if (num <= 0) num = 1;
                tvNum.setText(String.valueOf(num));
            }
            break;
            case R.id.tv_add: {
                if (TextUtils.isEmpty(tvNum.getText())) {
                    tvNum.setText("1");
                    return;
                }
                int num = Integer.valueOf(tvNum.getText().toString()) + 1;
                int max = specListModel == null ? goodsModel.getDetail().getStock() : specListModel.getForm().getStock_num();
                if (num > max) {
                    ToastUtil.getInstance().show(getActivity(), "数量超出范围");
                    return;
                }
                if (num <= 0) num = 1;
                tvNum.setText(String.valueOf(num));
            }
            break;
            case R.id.tv_enter:
                enter();
                break;
        }
    }


    private void enter() {
        if (goodsModel == null) return;
        if (goodsModel.getSpecData() != null && specListModel == null) {
            ToastUtil.getInstance().show(getActivity(), "请选择");
            return;
        }
        if (TextUtils.isEmpty(tvNum.getText().toString())) {
            ToastUtil.getInstance().show(getActivity(), "请选择商品数量");
            return;
        }
        int num = Integer.valueOf(tvNum.getText().toString());
        if (num == 0) {
            ToastUtil.getInstance().show(getActivity(), "请选择商品数量");
            return;
        }
        ((GoodsActivity) getActivity()).buy(specListModel, num, addCart);
        dismiss();
    }


    private GoodsModel cacheGoodsModel;
    private GoodsModel goodsModel;
    private boolean addCart;
    private View mainView;

    public void setData(GoodsModel goodsModel, boolean addCart, View mainView) {
        this.addCart = addCart;
        this.mainView = mainView;
        if (gsv == null) {
            this.cacheGoodsModel = goodsModel;
            return;
        }
        tvEnter.setText(addCart ? "确认" : "立即购买");
        if (this.goodsModel == goodsModel) return;
        this.goodsModel = goodsModel;


        if (goodsModel == null || goodsModel.getSpecData() == null)
            gsv.setVisibility(View.GONE);
        else
            gsv.setData(goodsModel.getSpecData().getSpec_attr(), goodsModel.getSpecData().getSpec_list());
        gsv.postDelayed(new Runnable() {
            @Override
            public void run() {
                int mainHeight = rlMainView.getMeasuredHeight();
                int gsvHeight = gsv.getMeasuredHeight();
                int otherHeight = CommonUtils.dip2px(mContext, 290);
                if (mainHeight < (gsvHeight + otherHeight)) {
                    ViewGroup.LayoutParams layoutParams = scrollView.getLayoutParams();
                    layoutParams.height = mainHeight - otherHeight;
                    scrollView.setLayoutParams(layoutParams);
                }
                rlMainView.setVisibility(View.VISIBLE);
            }
        }, 100);
        onGoodsSelect(null, null);
    }


    private SpecListModel specListModel;

    @Override
    public void onGoodsSelect(GoodsSelectView view, SpecListModel model) {
        this.specListModel = model;
        if (specListModel == null) {
            tvPrice.setText(PriceUtils.getPriceText2(goodsModel.getDetail().getGoods_price()));
            tvRemaining.setText(String.valueOf(goodsModel.getDetail().getStock()));
            HCUtils.loadWebImg(getActivity(), ivImg, goodsModel.getDetail().getImage());
            tvNum.setText("1");
            if (goodsModel.getDetail().getStock() > 0) {
                tvEnter.setBackgroundColor(getResources().getColor(R.color.main_color));
                tvEnter.setText(addCart ? "确认" : "立即购买");
                tvEnter.setEnabled(true);
            } else {
                tvEnter.setBackgroundColor(getResources().getColor(R.color.bg_E8E8E8));
                tvEnter.setText("商品已售罄");
                tvEnter.setEnabled(false);
            }
        } else {
            tvPrice.setText(PriceUtils.getPriceText2(specListModel.getForm().getGoods_price()));
            tvRemaining.setText(String.valueOf(specListModel.getForm().getStock_num()));
            HCUtils.loadWebImg(getActivity(), ivImg, specListModel.getForm().getSpec_image());
            if (specListModel.getForm().getStock_num() > 0) {
                tvEnter.setBackgroundColor(getResources().getColor(R.color.main_color));
                tvEnter.setText(addCart ? "确认" : "立即购买");
                tvEnter.setEnabled(true);
            } else {
                tvEnter.setBackgroundColor(getResources().getColor(R.color.bg_E8E8E8));
                tvEnter.setText("商品已售罄");
                tvEnter.setEnabled(false);
            }
            if (TextUtils.isEmpty(tvNum.getText())) {
                tvNum.setText("1");
                return;
            }
            int num = Integer.valueOf(tvNum.getText().toString());
            if (num == 0) return;
            if (num > specListModel.getForm().getStock_num())
                tvNum.setText(String.valueOf(specListModel.getForm().getStock_num()));
        }
    }


    private Context getActivity() {
        return mContext;
    }

    private void dismiss() {
        setVisibility(GONE);
    }

    public void show() {
        setVisibility(VISIBLE);
    }
}
